//
//  NBADBManager+Coach.swift
//  NBA
//
//  Created by tiger on 2018/2/3.
//  Copyright © 2018年 Meigabay. All rights reserved.
//

import UIKit
import SQLite

internal class NBAClubCoachTableColumn: NBAWorkerTableColumn {
    //固定信息
    let clubId = Expression<String>("clubId")
}

extension NBADBManager {
    
    internal func createClubCoachTable() throws {
        let playerColumns = NBAClubCoachTableColumn()
        let createCoachTable = self.nbaCoachTable.create(ifNotExists: true) { (table) in
            table.column(playerColumns.workerId,primaryKey: true)
            table.column(playerColumns.avatar)
            table.column(playerColumns.name)
            table.column(playerColumns.nickname)
            table.column(playerColumns.gender)
            table.column(playerColumns.height)
            table.column(playerColumns.weight)
            table.column(playerColumns.birthDay)
            table.column(playerColumns.drafted)
            table.column(playerColumns.introduction)
            table.column(playerColumns.createTime)
            table.column(playerColumns.type)
            table.column(playerColumns.clubId)
        }
        do {
            try self.db.run(createCoachTable)
            LogUtil.i("created coach table")
        } catch {
            LogUtil.e(error)
            throw(error)
        }
    }
    
    
    /// 插入教练
    ///
    /// - Parameter coach: 教练
    /// - Throws: 异常抛出
    internal func insertClubCoach(coach: NBAClubCoach) throws {
        let columns = NBAClubBossTableColumn()
        let insertCoach = self.nbaCoachTable.insert(columns.workerId <- coach.workerId,
                                                  columns.clubId <- coach.clubId,
                                                  columns.avatar <- coach.avatar,
                                                  columns.name <- coach.name,
                                                  columns.nickname <- coach.nickname,
                                                  columns.gender <- coach.gender.rawValue,
                                                  columns.height <- coach.height,
                                                  columns.weight <- coach.weight,
                                                  columns.birthDay <- coach.birthDay,
                                                  columns.drafted <- coach.drafted,
                                                  columns.introduction <- coach.introduction,
                                                  columns.createTime <- Date().timeIntervalSince1970,
                                                  columns.type <- coach.type.rawValue)
        do {
            try self.db.run(insertCoach)
            LogUtil.i("Insert Coach Success")
        } catch {
            LogUtil.e(error)
            throw(error)
        }
    }
    
}
